<?php
class Controller_Users extends Controller_Base
{
	public function index()
	{
		$this->view->pageTitle = self::BASE_PAGE_TITLE.' --- Users';
		$users = new Model_DbTable_Users();
		//var_dump($users);
		$usersArr = $users->fetchAll();
		//var_dump($usersArr);
		$this->view->firstName = $usersArr[1]->firstName;
		var_dump($this->view->firstName);
	}
	
	public function register()
	{
		$this->view->pageTitle = self::BASE_PAGE_TITLE.' --- User Register';
		$this->view->title = $this->view->pageTitle;
		
		$this->view->postData = (object)array(
			'firstName' => 'first name',
			'lastName' => 'last name',
			'email' => 'email@email.com',
			
		);
		
		$this->view->errorMessage = '';
		if($this->request->isPost()){
			$model = new Model_User();
			$postData = $this->request->getPost();
			$this->view->postData = (object)$postData;
			$validationData = $model->getRegisterValidationData($postData);
			if(!$validationData->isValid){
				$this->view->errorMessage = $validationData->message;
				return;
			}
			if(!$model->save($postData)){
				$this->view->errorMessage = 'Faild to register try again later';
				return;
			}
			$this->_forward(array('act' => 'login'));
		}
	}
	
	public function login()
	{
		$this->view->pageTitle = self::BASE_PAGE_TITLE.' --- User Login';
		$this->view->title = $this->view->pageTitle;
		$this->view->errorMessage = '';
		if($this->request->isPost()){
			$pwd = (string)$this->request->getParam('password');
			$email = (string)$this->request->getParam('email');
			$model = new Model_User();
			$isValid = $model->isValid($email, $pwd); 
			if(!$isValid){
				$this->view->errorMessage = 'User name or password is invalid';
				return;
			}
			$this->redirect(array('ctrl' => 'index', 'act' => 'index'));
		}
	}
	
	public function logout()
	{
		HackerU_Session::getInstance()->destroy();
		$this->redirect(array('ctrl' => 'index', 'act' => 'index'));
	}
	
	public function csv()
	{
		$this->disabledView = true;
		$model = new Model_DbTable_Users();
		$users = 	$model->fetchAll();
		$csvStr = 'first name, last name, email, cell phone'.PHP_EOL;
		foreach ($users as $user){
			$csvStr .= $user->firstName.','.$user->lastName.','.$user->email.','.$user->cellPhone.PHP_EOL;
		}
		header("Content-type: text/csv; charset=UTF-8");
		header("Content-Disposition: attachment; filename=users.csv");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $csvStr;
	}
	
	public function jsonLists()
	{
		$this->disabledView = true;
		$model = new Model_DbTable_Users();
		$users = $model->fetchAll();
		echo json_encode($users);
	}
	
	public function lists()
	{
		$this->view->pageTitle = self::BASE_PAGE_TITLE.' --- Users List';
		$this->view->title = $this->view->pageTitle;
		
		$model = new Model_DbTable_Users();
		$this->view->users = $model->fetchAll();
	}
	
	public function xml()
	{
		$this->disabledView = true;
		$dom = new DOMDocument('1.0', 'utf-8');
		
		$model = new Model_DbTable_Users();
		$usersList = $model->fetchAll();
		
		$users = $dom->createElement('users');
		
		foreach ($usersList as $user){
			$userTag = $dom->createElement('user');
			
			$id = $dom->createElement('id', $user->id);
			$userTag->appendChild($id);
			
			$firstName = $dom->createElement('firstName', $user->firstName);
			$userTag->appendChild($firstName);
			
			$lastName = $dom->createElement('lastName', $user->lastName);
			$userTag->appendChild($lastName);
			
			$users->appendChild($userTag);
		}
		
		$dom->appendChild($users);
		header ("Content-Type:text/xml");
		echo $dom->saveXML();
	}
}